package toy.keli.picmap.util;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import toy.keli.picmap.model.system;
import toy.keli.picmap.model.time;

//摘自： android 压缩多个文件夹,android文件或文件夹压缩  https://blog.csdn.net/weixin_32200879/article/details/117290765
public class utilZip {
    private static final int BUFFER = 1024 * 1024;
    ZipOutputStream out;
    File outFile;



    /**
     * 压缩文件
     */
    private void compress2File(File file,    String basedir) {
        if (!file.exists()) {
            return;
        }
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(
                    new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            out.putNextEntry(entry);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        } catch (Exception e) {
            Log.d("kdebug",""+e.getMessage());
        }finally {
            try {
                bis.close();
            } catch (IOException e) {
                Log.d("kdebug",""+e.getMessage());
            }
        }
    }
    public void compress(File file,String basedir) {
        if (file.isFile()) {
            this.compress2File(file,basedir);
        } else if(file.exists()) {
            File[] files = file.listFiles();
            if(files!=null && files.length !=0){
                for (int i = 0; i < files.length; i++) {
                    compress(files[i],  basedir +file.getName() + "/");
                }
            }
        }
    }

    public utilZip(String file){
        outFile=new File(file);
        File fileParentDir = outFile.getParentFile();
        if (!fileParentDir.exists()) {
            fileParentDir.mkdirs();
        }
        if(!outFile.exists() || system.file.size(file)==0)
        try {
            //文件操作 小结  http://blog.sina.com.cn/s/blog_48e2ea3401012el3.html
            out=new ZipOutputStream(new FileOutputStream(outFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public boolean add(String file){
        compress(new File(file),"./");
        return true;
    }
    public boolean save(){
        try {
            out.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private void extract(String to) {
        File fileParentDir = new File(to);
        if (!fileParentDir.exists()) {
            fileParentDir.mkdirs();
        }
        try {
            ZipFile zf = new ZipFile(outFile);
            for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
                ZipEntry entry = ((ZipEntry)entries.nextElement());
                InputStream in = zf.getInputStream(entry);

                String toFile = to  + entry.getName();
                File desFile = new File(toFile); desFile.createNewFile();
                OutputStream out = new FileOutputStream(desFile);

                byte buffer[] = new byte[BUFFER];   int realLength;
                while ((realLength = in.read(buffer)) > 0) {
                    out.write(buffer, 0, realLength);
                }

                in.close();
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 压缩
     * @param zipFile   压缩后的文件
     * @param files     需要压缩的文件
     * @return
     */
    public static boolean compress(String zipFile ,String[] files) {
            return compress(zipFile,files,(i)->{});
    }
   public static boolean compress(String zipFile ,String[] files,utilCallback.index func) {
        File zf0=new File(zipFile);
        if(zf0.exists()) zf0.delete();
        utilZip zf = new utilZip(zipFile);
        for(int i=0;i<files.length;i++) {
            zf.add(files[i]);
            func.call(i);
        }
        return zf.save();
    }



    /**
     *  解压缩
     * @param file 需要解压的文件
     * @param to   解压到
     * @param o     完成之后
     */
    public static void unCompress(String file, String to, utilCallback.func o) {
        utilZip zf = new utilZip(file);
        time.Out(10,()->{
            zf.extract(to);
            o.call();
        });
    }
}
